package com.example._13redis_session.controller;

import com.example._13redis_session.pojo.User;
import com.example._13redis_session.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @auther 0785fanlei.
 * @date 2022/4/8 16:11
 */

@Controller
public class UserController
{
    @Autowired
    private IUserService userService;

    @RequestMapping("index")
    public String index()
    {
        return "index";
    }

    @RequestMapping("login")
    public String login()
    {
        return "login";
    }

    @RequestMapping("logout")
    public String logout(HttpSession session)
    {
        session.invalidate();
        return "redirect:/login";
    }

    @RequestMapping("loginchk")
    public String loginchk(String username, String password, HttpSession session)
    {
        User user = userService.loginchk(username,password);

        if(user!=null)
        {
            System.out.println("登录成功");
            session.setAttribute("user",user);
            return "redirect:/index";
        }
        else
        {
            System.out.println("登录失败");
            return "redirect:/login";
        }
    }
}
